﻿/// <reference path="_namespace.js" />
/// <reference path="Vector3.js" />
/// <reference path="/ajax/xna/framework/BoundingSphere.js"/>
/// <reference path="/ajax/xna/framework/PlaneIntersectionType.js"/>
/*****************************************************************************
Ajax library class definition. Kevin Robinson 2008.
Class: ajax.math.Plane3
/*****************************************************************************/

ajax.lang.addNamespace("ajax.math");
$imports("ajax.math.Vector3", "ajax.xna.framework.PlaneIntersectionType");

ajax.math.Plane3 = function(vectorPlaneNormal, floatPlaneDistance)
{    
    this.normal     = vectorPlaneNormal;
    this.distance   = floatPlaneDistance; 
}

// Statics
ajax.math.Plane3.cast = function(obj)
{///<returns type="ajax.math.Plane3"/>
    return obj;
}

// Public properties
ajax.math.Plane3.prototype.normal   = ajax.math.Vector3.prototype
ajax.math.Plane3.prototype.distance = 0;

// Public methods
ajax.math.Plane3.prototype.reflectionVector = function(vec3)
{///<summary>Calculates the reflection vector of the vector passed when bounced off this Plane3.</summary>
///<returns type="ajax.math.Vector3"/>
    return ajax.math.Vector3.reflectionVector(vec3, this.normal);
}

// Static methods

ajax.math.Plane3.createFrom = function(vectorLine, vectorPointOnPoly, vectorUp)
{///<summary>Alternative to constructor overload</summary>
    var normal      = ajax.math.Vector3.normalize(ajax.math.Vector3.cross(vectorLine, vectorUp));
    var distance    = ajax.math.Vector3.dot(vectorPointOnPoly, normal);     
    var result      = new ajax.math.Plane3(normal, distance);
    return result;
}

ajax.math.Plane3.intersectsBoundingSphere = function(plane, sphere)
{///<param name="plane" type="ajax.math.Plane3"/>
 ///<param name="sphere" type="ajax.xna.framework.BoundingSphere"/>
 ///<returns type="ajax.xna.framework.PlaneIntersectionType"/>
    sphere = ajax.xna.framework.BoundingSphere.cast(sphere);
    
    var num2 = ((sphere.center.x * plane.normal.x) + (sphere.center.y * plane.normal.y)) + (sphere.center.z * plane.normal.z);
    var num = num2 + plane.distance;
    if (num > sphere.radius)
    {
        return ajax.xna.framework.PlaneIntersectionType.Front;
    }
    if (num < -sphere.radius)
    {
        return ajax.xna.framework.PlaneIntersectionType.Back;        
    }
    return ajax.xna.framework.PlaneIntersectionType.Intersecting;    
}

ajax.math.Plane3.prototype.intersectsBoundingSphere = function(sphere)
{ ///<param name="sphere" type="ajax.xna.framework.BoundingSphere"/>
  ///<returns type="ajax.xna.framework.PlaneIntersectionType"/>    
    return ajax.math.Plane3.intersectsBoundingSphere(this, sphere);    
}

ajax.math.Plane3.normalize = function(value)
{///<summary>Changes the coefficients of the Plane.Normal vector of a Plane to make it of unit length.</summary>
 ///<param name="value" type="ajax.math.Plane3">The Plane to normalize.</param>
 ///<returns type="ajax.math.Plane3"/>
    var plane;
    var normal;
    var num2 = ((value.normal.x * value.normal.x) + (value.normal.y * value.normal.y)) + (value.normal.z * value.normal.z);
    if (Math.abs(parseFloat(num2 - 1)) < 1.192093E-07)
    {
        normal = ajax.math.Vector3.createFrom(value.normal);  // Create copy of reference type    
        plane = new ajax.math.Plane3(normal, value.distance);
        return plane;        
    }
    var num = 1 / (parseFloat(Math.sqrt(parseFloat(num2))));
    
    normal = ajax.math.Vector3.createFrom(value.normal); // Create copy of reference type
    normal.multiplyScalar(num);    
    plane = new ajax.math.Plane3(normal, value.distance * num);
    return plane;
}

ajax.math.Plane3.prototype.normalize = function()
{
    var result = ajax.math.Plane3.normalize(this);
    this.normal = result.normal;
    this.distance = result.distance;
}

ajax.math.Plane3.prototype.toString = function()
{
    return String.toFormat("Normal:{0} Distance:{1}", this.normal, this.distance);   
}




